# -*- coding: utf-8 -*-
import serial, win32api, win32con

SCR_SIZE = (1920, 1080)
BT_DOWN=0; if_held={'lf':False, 'up':False}; if_pause=False

with serial.Serial('com3') as ser:        #这里修改成你的端口号
    ser.baudrate = 57600; ser.readline()  #设置波特率并读取一行
    while True:
        data = ser.readline().split(',')
        data = [float(i) for i in data]   #列表推导式缩短篇幅
        button_status = {'lf':data[3], 'ri':data[4], 'up':data[5]}
        #认为按下开关后松开是一次单击，该键单击则启用或暂停控制鼠标位置
        if button_status['up'] == BT_DOWN and not if_held['up']:
            if_held['up'] = True                                  
        elif button_status['up'] != BT_DOWN and if_held['up']:    
            if_held['up'] = False; if_pause = not if_pause
        #检测该键状态，并模拟鼠标左键操作
        if button_status['lf'] == BT_DOWN and not if_held['lf']:
            if_held['lf'] = True
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0)
        elif button_status['lf'] != BT_DOWN and if_held['lf']:
            if_held['lf'] = False
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0)
        #启用或暂停对鼠标关闭位置的控制
        if not if_pause:  
            x, y = data[2], data[1]  
            win32api.SetCursorPos((int(x*SCR_SIZE[0]), int(y*SCR_SIZE[1])))
        
        
